﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using DevTrends.MvcDonutCaching;
using ECG.Common;
using ECG.DAL;
using ECG.WebUI.Models;
using System.Xml.Linq;
using System;

namespace ECG.WebUI.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        //[DonutOutputCache(Duration = 36000, VaryByParam = "categoryName;categoryId;page")]
        public ActionResult Index(string categoryName = "", string categoryId = "", int page = 1, int sortdrop = 0, string catefil = "OEJHY", string price = "", string rate = "", string provider = "", string color = "", string size = "", string attr = "")
        {
            var itemCate = ProductCategoryDb.GetById(categoryId) ?? new ProductCategoryDto();
            ViewBag.listCateMapLink = ProductCategoryMapLinkDb.ShowBYCate(categoryId);
            var str = string.Empty;
            if (page != 1) str = " | trang " + page;
            ViewBag.strPage = str;
            ViewBag.Fillter = sortdrop;
            ViewBag.CateFill = catefil;
            ViewBag.ListBanner = ProductCategoryBannerDb.GetListBannerByCategory(categoryId) ?? new List<ProductCategoryBannerHomeDto>();
            var CateModel = new CategoryFilterModel
            {
                CategoryId = categoryId,
                PriceMin = !string.IsNullOrEmpty(price)?price.Split(';')[0]:"",
                PriceMax = !string.IsNullOrEmpty(price) ? price.Split(';')[1] : "",
                RateMin = !string.IsNullOrEmpty(rate) ? rate.Split(';')[0] : "",
                RateMax = !string.IsNullOrEmpty(rate) ? rate.Split(';')[1] : "",
                Providers = provider,
                Colors = color,
                Sizes = size,
                Attrs = attr
            };
            ViewBag.CategoryFilModel = CateModel;

            return View(itemCate);
        }

        //  Product Filter by Category - Bo loc
        public PartialViewResult ProductFilterCategory(CategoryFilterModel modelFil)
        {
            var model = new ConfigFilterModel();
            try
            {
                #region Read XML
                XDocument xmlDoc = XDocument.Load(ConfigurationManager.AppSettings["HostContainImages"] + "/ECGfilterconfig.xml");
                var iTemNode = from x in xmlDoc.Descendants("rows") select x;
                var item = iTemNode.FirstOrDefault();

                model.activePricRange = Convert.ToBoolean(item.Element("activePricRange").Value);
                model.activeProvider = Convert.ToBoolean(item.Element("activeProvider").Value);
                model.activeColor = Convert.ToBoolean(item.Element("activeColor").Value);
                model.activeSize = Convert.ToBoolean(item.Element("activeSize").Value);
                model.activeRate = Convert.ToBoolean(item.Element("activeRate").Value);
                model.activeAttr = Convert.ToBoolean(item.Element("activeAttr").Value);

                model.priceRangeMin = Convert.ToDecimal(item.Element("priceRangeMin").Value);
                model.priceRangeMinDefault = Convert.ToDecimal(item.Element("priceRangeMinDefault").Value);
                model.priceRangeMax = Convert.ToDecimal(item.Element("priceRangeMax").Value);
                model.priceRangeMaxDefault = Convert.ToDecimal(item.Element("priceRangeMaxDefault").Value);

                model.rangeNumber = Convert.ToInt32(item.Element("rangeNumber").Value);

                model.applyTypePriceRange = item.Element("applyTypePriceRange").Value;
                model.applyTypeProvider = item.Element("applyTypeProvider").Value;
                model.applyTypeColor = item.Element("applyTypeColor").Value;
                model.applyTypeSize = item.Element("applyTypeSize").Value;
                model.applyTypeRate = item.Element("applyTypeRate").Value;
                model.applyTypeAttr = item.Element("applyTypeAttr").Value;

                model.CategoriesPriceRange = item.Element("CategoriesPriceRange").Value;
                model.CategoriesProvider = item.Element("CategoriesProvider").Value;
                model.CategoriesColor = item.Element("CategoriesColor").Value;
                model.CategoriesSize = item.Element("CategoriesSize").Value;
                model.CategoriesRate = item.Element("CategoriesRate").Value;
                model.CategoriesAttr = item.Element("CategoriesAttr").Value;

                model.showNumberLineProvider = Convert.ToInt32(item.Element("showNumberLineProvider").Value);
                model.showNumberLineColor = Convert.ToInt32(item.Element("showNumberLineColor").Value);
                model.showNumberLineSize = Convert.ToInt32(item.Element("showNumberLineSize").Value);
                model.showNumberLineAttr = Convert.ToInt32(item.Element("showNumberLineAttr").Value);

                model.ShowNameProvider = Convert.ToBoolean(item.Element("ShowNameProvider").Value);
                model.ShowNameColor = Convert.ToBoolean(item.Element("ShowNameColor").Value);
                #endregion
            }
            catch { }
            //modelFil.PriceMin = modelFil.PriceMin.Replace(".", "");
            //modelFil.PriceMax = modelFil.PriceMax.Replace(".", "");
            //  Price fil
            if (model.activePricRange)
            {
                if (model.applyTypePriceRange.Equals("0") || model.applyTypePriceRange.Equals("2") || model.applyTypePriceRange.Equals("4"))
                {
                    var arrCate = model.CategoriesPriceRange.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        var modelPriceDefault = new CategoryFilterModel {
                            CategoryId = modelFil.CategoryId
                        };
                        ViewBag.PriceFilDefault = ProductDb.GetPriceFilter(modelPriceDefault) ?? new List<PriceFilterDto>();
                        // khoang gia da duoc loc
                        ViewBag.PriceFil = ProductDb.GetPriceFilter(modelFil) ?? new List<PriceFilterDto>();
                    }
                }
            }

            //  Rate fil
            if (model.activeRate)
            {
                if (model.applyTypeRate.Equals("0") || model.applyTypeRate.Equals("2") || model.applyTypeRate.Equals("4"))
                {
                    var arrCate = model.CategoriesRate.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        var modelRateDefault = new CategoryFilterModel
                        {
                            CategoryId = modelFil.CategoryId
                        };
                        ViewBag.RateFilDefault = RateDb.GetRateFilter(modelRateDefault) ?? new List<RateFilterDto>();
                        ViewBag.RateFil = RateDb.GetRateFilter(modelFil) ?? new List<RateFilterDto>();
                    }
                }
            }

            //  Provider fil
            if (model.activeProvider)
            {
                if (model.applyTypeProvider.Equals("0") || model.applyTypeProvider.Equals("2") || model.applyTypeProvider.Equals("4"))
                {
                    var arrCate = model.CategoriesProvider.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.ProviderFil = ProviderDb.GetProviderFilter(modelFil) ?? new List<ProviderFilterDto>();
                    }
                }
            }

            // Color fil
            if (model.activeColor)
            {
                if (model.applyTypeColor.Equals("0") || model.applyTypeColor.Equals("2") || model.applyTypeColor.Equals("4"))
                {
                    var arrCate = model.CategoriesColor.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.ColorFil = ColorDb.GetColorFilter(modelFil) ?? new List<ColorFilterDto>();
                    }
                }
            }

            // Size fil
            if (model.activeSize)
            {
                if (model.applyTypeSize.Equals("0") || model.applyTypeSize.Equals("2") || model.applyTypeSize.Equals("4"))
                {
                    var arrCate = model.CategoriesSize.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.SizeFil = SizeDb.GetSizeFilter(modelFil) ?? new List<SizeFilterDto>();
                    }
                }
            }

            // Attr fil
            if (model.activeAttr)
            {
                if (model.applyTypeAttr.Equals("0") || model.applyTypeAttr.Equals("2") || model.applyTypeAttr.Equals("4"))
                {
                    var arrCate = model.CategoriesAttr.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.AttrFil = AttributeDb.GetAttrFilter(modelFil) ?? new List<AttrFilterDto>();
                    }
                }
            }
            ViewBag.modelFil = modelFil;
            return PartialView(model);
        }

        public PartialViewResult AjaxProductFilterCategory(string CategoryId="", string price = "", string rate = "", string provider = "", string color = "", string size = "", string attr = "")
        {
            var model = new ConfigFilterModel();
            var modelFil = new CategoryFilterModel
            {
                CategoryId = CategoryId,
                PriceMin = !string.IsNullOrEmpty(price) ? price.Split(';')[0] : "",
                PriceMax = !string.IsNullOrEmpty(price) ? price.Split(';')[1] : "",
                RateMin = !string.IsNullOrEmpty(rate) ? rate.Split(';')[0] : "",
                RateMax = !string.IsNullOrEmpty(rate) ? rate.Split(';')[1] : "",
                Providers = provider,
                Colors = color,
                Sizes = size,
                Attrs = attr
            };
            try
            {
                #region Read XML
                XDocument xmlDoc = XDocument.Load(ConfigurationManager.AppSettings["HostContainImages"] + "/ECGfilterconfig.xml");
                var iTemNode = from x in xmlDoc.Descendants("rows") select x;
                var item = iTemNode.FirstOrDefault();

                model.activePricRange = Convert.ToBoolean(item.Element("activePricRange").Value);
                model.activeProvider = Convert.ToBoolean(item.Element("activeProvider").Value);
                model.activeColor = Convert.ToBoolean(item.Element("activeColor").Value);
                model.activeSize = Convert.ToBoolean(item.Element("activeSize").Value);
                model.activeRate = Convert.ToBoolean(item.Element("activeRate").Value);
                model.activeAttr = Convert.ToBoolean(item.Element("activeAttr").Value);

                model.priceRangeMin = Convert.ToDecimal(item.Element("priceRangeMin").Value);
                model.priceRangeMinDefault = Convert.ToDecimal(item.Element("priceRangeMinDefault").Value);
                model.priceRangeMax = Convert.ToDecimal(item.Element("priceRangeMax").Value);
                model.priceRangeMaxDefault = Convert.ToDecimal(item.Element("priceRangeMaxDefault").Value);

                model.rangeNumber = Convert.ToInt32(item.Element("rangeNumber").Value);

                model.applyTypePriceRange = item.Element("applyTypePriceRange").Value;
                model.applyTypeProvider = item.Element("applyTypeProvider").Value;
                model.applyTypeColor = item.Element("applyTypeColor").Value;
                model.applyTypeSize = item.Element("applyTypeSize").Value;
                model.applyTypeRate = item.Element("applyTypeRate").Value;
                model.applyTypeAttr = item.Element("applyTypeAttr").Value;

                model.CategoriesPriceRange = item.Element("CategoriesPriceRange").Value;
                model.CategoriesProvider = item.Element("CategoriesProvider").Value;
                model.CategoriesColor = item.Element("CategoriesColor").Value;
                model.CategoriesSize = item.Element("CategoriesSize").Value;
                model.CategoriesRate = item.Element("CategoriesRate").Value;
                model.CategoriesAttr = item.Element("CategoriesAttr").Value;

                model.showNumberLineProvider = Convert.ToInt32(item.Element("showNumberLineProvider").Value);
                model.showNumberLineColor = Convert.ToInt32(item.Element("showNumberLineColor").Value);
                model.showNumberLineSize = Convert.ToInt32(item.Element("showNumberLineSize").Value);
                model.showNumberLineAttr = Convert.ToInt32(item.Element("showNumberLineAttr").Value);

                model.ShowNameProvider = Convert.ToBoolean(item.Element("ShowNameProvider").Value);
                model.ShowNameColor = Convert.ToBoolean(item.Element("ShowNameColor").Value);
                #endregion
            }
            catch { }
            //  Price fil
            if (model.activePricRange)
            {
                if (model.applyTypePriceRange.Equals("0") || model.applyTypePriceRange.Equals("2") || model.applyTypePriceRange.Equals("4"))
                {
                    var arrCate = model.CategoriesPriceRange.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        var modelPriceDefault = new CategoryFilterModel
                        {
                            CategoryId = modelFil.CategoryId
                        };
                        ViewBag.PriceFilDefault = ProductDb.GetPriceFilter(modelPriceDefault) ?? new List<PriceFilterDto>();
                        ViewBag.PriceFil = ProductDb.GetPriceFilter(modelFil) ?? new List<PriceFilterDto>();
                    }
                }
            }

            //  Rate fil
            if (model.activeRate)
            {
                if (model.applyTypeRate.Equals("0") || model.applyTypeRate.Equals("2") || model.applyTypeRate.Equals("4"))
                {
                    var arrCate = model.CategoriesRate.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        var modelRateDefault = new CategoryFilterModel
                        {
                            CategoryId = modelFil.CategoryId
                        };
                        ViewBag.RateFilDefault = RateDb.GetRateFilter(modelRateDefault) ?? new List<RateFilterDto>();
                        ViewBag.RateFil = RateDb.GetRateFilter(modelFil) ?? new List<RateFilterDto>();
                    }
                }
            }

            //  Provider fil
            if (model.activeProvider)
            {
                if (model.applyTypeProvider.Equals("0") || model.applyTypeProvider.Equals("2") || model.applyTypeProvider.Equals("4"))
                {
                    var arrCate = model.CategoriesProvider.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.ProviderFil = ProviderDb.GetProviderFilter(modelFil) ?? new List<ProviderFilterDto>();
                    }
                }
            }

            // Color fil
            if (model.activeColor)
            {
                if (model.applyTypeColor.Equals("0") || model.applyTypeColor.Equals("2") || model.applyTypeColor.Equals("4"))
                {
                    var arrCate = model.CategoriesColor.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.ColorFil = ColorDb.GetColorFilter(modelFil) ?? new List<ColorFilterDto>();
                    }
                }
            }

            // Size fil
            if (model.activeSize)
            {
                if (model.applyTypeSize.Equals("0") || model.applyTypeSize.Equals("2") || model.applyTypeSize.Equals("4"))
                {
                    var arrCate = model.CategoriesSize.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.SizeFil = SizeDb.GetSizeFilter(modelFil) ?? new List<SizeFilterDto>();
                    }
                }
            }

            // Attr fil
            if (model.activeAttr)
            {
                if (model.applyTypeAttr.Equals("0") || model.applyTypeAttr.Equals("2") || model.applyTypeAttr.Equals("4"))
                {
                    var arrCate = model.CategoriesAttr.Split(';');

                    var chk = -1;
                    if (arrCate != null && arrCate.Length > 0)
                    {
                        chk = Array.IndexOf(arrCate, modelFil.CategoryId);
                    }
                    // kiem tra xem danh muc hien tai dc cho phep hien thi hay ko
                    if (chk > -1)
                    {
                        ViewBag.AttrFil = AttributeDb.GetAttrFilter(modelFil) ?? new List<AttrFilterDto>();
                    }
                }
            }
            ViewBag.modelFil = modelFil;
            return PartialView(model);
        }

        public ActionResult ProductByCate(string categoryName = "", string categoryId = "", int page = 1, string discount = "", string brandId = "")
        {
            //var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["ProductListByCateIdPageSize"]);
            //var totalPage = 0;
            //var model = ProductDb.GetAllProductByCategoryId(page, itemsPerPage, ref totalPage, categoryId) ?? new List<ProductByCateTopListPagingDto>();
            //ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };

            var cateItem = ProductCategoryDb.GetInfotext(categoryId);
            ViewBag.CateName = cateItem.Name;
            ViewBag.CategoryId = categoryId;
            ViewBag.CateSUmaryHeader = cateItem.SummaryCate;
            return PartialView();
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryName;categoryId;page")]
        public ActionResult ProductByCateL2(string categoryName = "", string categoryId = "", int page = 1, string discount = "", string brandId = "")
        {
            //var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["ProductListByCateIdPageSize"]);
            //var totalPage = 0;
            //var model = ProductDb.GetAllProductByCategoryId(page, itemsPerPage, ref totalPage, categoryId) ?? new List<ProductByCateTopListPagingDto>();
            //ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };

            var cateItem = ProductCategoryDb.GetInfotext(categoryId);
            ViewBag.CateName = cateItem.Name;
            ViewBag.CategoryId = categoryId;
            ViewBag.CateSUmaryHeader = cateItem.SummaryCate;
            return PartialView();
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryId")]
        public ActionResult DirectionCategory(string categoryId)
        {
            var model = ProductCategoryDb.GetAllByIdAndBreadCrumb(categoryId) ?? new List<ProductCategoryDto>();
            return PartialView(model);
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryName;categoryId")]
        public ActionResult ProductDiscount(string categoryName = "", string categoryId = "")
        {
            ViewBag.Count = ProductDb.GetProductDiscountBycate(categoryId);
            ViewBag.CategoryName = categoryName;
            ViewBag.CategoryId = categoryId;
            return PartialView();
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryId")]
        public ActionResult CategoryMenuLeft(string categoryId)
        {
            var menuList = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
            ViewBag.ItemCate = menuList.Single(m => m.Id == categoryId);
            return PartialView(menuList);
        }

        [DonutOutputCache(CacheProfile = "OneDay")]
        public ActionResult CategoryMenuLeftAll()
        {
            var menuList = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
            return PartialView(menuList);
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "action;controller")]
        public ActionResult MenuTwoLevel(string action, string controller)
        {
            var model = ProductCategoryDb.GetAll() ?? new List<ProductCategoryDto>();
            ViewBag.Action = action;
            ViewBag.Controler = controller;
            return PartialView(model);
        }

        //public PartialViewResult Top3ProductHot(string categoryId)
        //{
        //    var data = ProductDb.GetTop3ProductByCategory(categoryId) ?? new List<ProductMenuHomeDto>();
        //    var p4Cate = ProductDb.GetTop4NewByCategoryID(categoryId) ?? new List<ProductMenuHomeDto>();
        //    ViewBag.Top4NewProductCate = p4Cate;
        //    ViewBag.CategoryId = categoryId;
        //    return PartialView(data);
        //}

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryId;depth")]
        public PartialViewResult GetParentCate(string categoryId, int depth)
        {
            var data = new List<ProductCategoryParentCateDto>();
            if (depth == 2)
            {
                data = ProductCategoryDb.GetCategoryByParent(categoryId) ?? new List<ProductCategoryParentCateDto>();
            }
            ViewBag.CategoryId = categoryId;
            return PartialView(data);
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryId")]
        public PartialViewResult GetProductByCate(string categoryId)
        {
            var pList = ProductDb.GetTopProductByCateId(categoryId) ?? new List<ProductByCateTopListDto>();
            return PartialView(pList);
        }


        [DonutOutputCache(Duration = 36000, VaryByParam = "category;w;o")]
        public PartialViewResult SlideByCategory(string category, int w, int o)
        {
            var bList = ProductCategoryBannerDb.GetListBannerByWidth(category, w, o) ?? new List<ProductCategoryBannerHomeDto>();
            return PartialView(bList);
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "category;w;o")]
        public PartialViewResult AdvByCategory(string category, int w, int o)
        {
            var bList = ProductCategoryBannerDb.GetListBannerByWidth(category, w, o) ?? new List<ProductCategoryBannerHomeDto>();
            return PartialView(bList);
        }

        [DonutOutputCache(Duration = 36000, VaryByParam = "category;w;o")]
        public PartialViewResult Adv3ByCategory(string category, int w, int o)
        {
            var bList = ProductCategoryBannerDb.GetListBannerByWidth(category, w, o) ?? new List<ProductCategoryBannerHomeDto>();
            return PartialView(bList);
        }

        //babo new: Category promotion l2
        [DonutOutputCache(Duration = 36000, VaryByParam = "categoryId")]
        public PartialViewResult CategoryPromotionL2(string categoryId)
        {
            var data = ProductDb.GetTop3ProductByCategory(categoryId) ?? new List<ProductMenuHomeDto>();
            ViewBag.CategoryId = categoryId;
            return PartialView(data);
        }

        public PartialViewResult CategoryProductL2(string categoryId, int page = 1, int sortdrop = 0, string catefil = "OEJHY",
            string price = "", string rate = "", string provider = "", string color = "", string size = "", string attr = "")
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["ProductListByCateIdPageSize"]);
            var totalPage = 0;
            var totalRecord = 0;
            var model = new List<ProductRateByCateTopListPagingDto>();
            if (catefil == "ACCEH")
            {
                // cac san pham ban chay
                model = ProductDb.SelectProductByFilter_banchay(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "GJUYR")
            {
                // cac san pham khuyen mai
                model = ProductDb.SelectProductByFilter_khyenmai(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "OEJHY" || catefil == "FILL") //tat ca sp
            {
                // cac san pham moi nhat
                model = ProductDb.SelectProductByFilter_moinhat(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.Category = categoryId;
            ViewBag.CurrentPage = page;
            ViewBag.Fillter = sortdrop;
            ViewBag.CateFill = catefil;
            ViewBag.TotalRecod = totalRecord;
            ViewBag.RecordByPage = model.Count();
            return PartialView(model);
        }

        public PartialViewResult CategoryTabSelect(string categoryId, int page = 1, int sortdrop = 0, string catefil = "OEJHY",
            string price = "", string rate = "", string provider = "", string color = "", string size = "", string attr = "")
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["ProductListByCateIdPageSize"]);
            var totalPage = 0;
            var totalRecord = 0;
            var model = new List<ProductRateByCateTopListPagingDto>();
            if (catefil == "ACCEH")
            {
                // cac san pham ban chay
                model = ProductDb.SelectProductByFilter_banchay(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "GJUYR")
            {
                // cac san pham khuyen mai
                model = ProductDb.SelectProductByFilter_khyenmai(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            else if (catefil == "OEJHY" || catefil == "FILL") //tat ca sp
            {
                // cac san pham moi nhat
                model = ProductDb.SelectProductByFilter_moinhat(page, itemsPerPage, ref totalPage, ref totalRecord, categoryId, sortdrop, price, rate, provider, color, size, attr) ?? new List<ProductRateByCateTopListPagingDto>();
            }
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.CurrentPage = page;
            ViewBag.TotalRecod = totalRecord;
            ViewBag.RecordByPage = model.Count();
            return PartialView(model);
        }
    }
}
